class PhotoSet < ActiveRecord::Base
  ITEMS_PER_PAGE = 8

  has_many :photos, :conditions => ['published = ?', '1'], :order => "position", :dependent => :destroy
  
  validates_presence_of :name
  validates_length_of :name,
    :within => 1..200
    
  after_validation :add_canonical_name
  
  def self.get_items(count = ITEMS_PER_PAGE, page = nil)
    page = cleaned_page(page)
    
    find(
      :all,
      :offset     => (page - 1) * count,
      :limit      => count,
      :order      => "created_at DESC"
    )
  end

  def self.get_counts(page_size = ITEMS_PER_PAGE)
    counts = count
    pages = ((counts - 1)/ page_size) + 1
    [counts, pages]
  end
  
  def add_canonical_name
    self.canonical_name = "#{Time.now.strftime('%Y-%m-%d')}-#{self.name.canonize}"
  end
  
  def canonical_name
    "#{self.created_at.strftime('%Y-%m-%d')}-#{self.name.canonize.untaint}"
  end
end
